package animation.sort.algorithms;

import animation.sort.SortAnimation;
import animation.core.AlgorithmMonitorModel;
import animation.core.MonitorModelImpl;
import animation.core.Monitoreable;


public class InsertionSort
        extends SortAnimation implements Monitoreable
{


    public String toString()
    {
        return "Inserion Sort";
    }

    protected void sort(int array[])
            throws Exception
    {
        monitorModel.setVariableValue("i", 1);
        for (int i = 1; i < array.length; i++) {
            monitorModel.setVariableValue("i", i);
            markBox(0, i);
            monitorModel.setVariableValue("j", i);
            for (int j = i; j >= 1 && updateVar("j", j) && greater(array, j - 1, j); j--) {
                swap(array, j, j - 1);
            }
        }
    }

    protected void registerSpecificVariables(AlgorithmMonitorModel monitorModel) {
        monitorModel.registerVariable("i", String.valueOf(0));
        monitorModel.registerVariable("j", String.valueOf(0));
    }

    private boolean updateVar(String name, Object value){
        monitorModel.setVariableValue(name, value);
        return true;
    }


}

